package com.galaxy.payment.controller;

import com.galaxy.common.core.domain.R;
import com.galaxy.common.mybatis.core.page.PageQuery;
import com.galaxy.common.mybatis.core.page.TableDataInfo;
import com.galaxy.payment.domain.bo.PaymentBo;
import com.galaxy.payment.domain.vo.PaymentRecordVo;
import com.galaxy.payment.service.IPaymentRecordService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.List;

/**
 * 支付记录接口
 */
@RequestMapping("/payment/record")
@RequiredArgsConstructor
@RestController
public class PaymentRecordController {

    private final IPaymentRecordService paymentRecordService;

    /**
     * 创建支付
     */
    @PostMapping("/create")
    public R<PaymentRecordVo> createPayment(@RequestBody @Valid PaymentBo bo) {
        return R.ok(paymentRecordService.createPayment(bo));
    }

    /**
     * 获取支付记录
     */
    @GetMapping("/{payNo}")
    public R<PaymentRecordVo> getPaymentRecord(@PathVariable String payNo) {
        return R.ok(paymentRecordService.getPaymentRecord(payNo));
    }

    /**
     * 根据主键查询
     */
    @GetMapping("/id/{id}")
    public R<PaymentRecordVo> queryById(@PathVariable Long id) {
        return R.ok(paymentRecordService.getPaymentRecordById(id));
    }

    /**
     * 分页查询
     */
    @GetMapping("/page")
    public TableDataInfo<PaymentRecordVo> queryPageList(PaymentBo bo, PageQuery pageQuery) {
        return paymentRecordService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询列表
     */
    @GetMapping("/list")
    public R<List<PaymentRecordVo>> queryList(PaymentBo bo) {
        return R.ok(paymentRecordService.queryList(bo));
    }

    /**
     * 新增
     */
    @PostMapping
    public R<PaymentBo> insertByBo(@RequestBody @Valid PaymentBo bo) {
        return R.ok(paymentRecordService.insertByBo(bo));
    }

    /**
     * 修改
     */
    @PutMapping
    public R<Boolean> updateByBo(@RequestBody @Valid PaymentBo bo) {
        return R.ok(paymentRecordService.updateByBo(bo));
    }

    /**
     * 批量删除
     */
    @DeleteMapping("/batch")
    public R<Boolean> deleteWithValidByIds(@RequestBody Collection<Long> ids, @RequestParam(defaultValue = "false") Boolean isValid) {
        return R.ok(paymentRecordService.deleteWithValidByIds(ids, isValid));
    }

    /**
     * 取消支付
     */
    @PostMapping("/cancel/{payNo}")
    public R<Boolean> cancelPayment(@PathVariable String payNo) {
        return R.ok(paymentRecordService.cancelPayment(payNo));
    }

    /**
     * 处理支付回调
     */
    @PostMapping("/callback")
    public R<Boolean> handlePaymentCallback(@RequestParam String payNo, @RequestParam String tradeNo, @RequestParam Integer status) {
        return R.ok(paymentRecordService.handlePaymentCallback(payNo, tradeNo, status));
    }
}
